<?php
namespace app\index\validate;
use think\Validate;
use think\Db;
class Ower extends Validate{
	public $plot;
	public $house;
	public $user;
    protected $rule = [
    	'tit'		=> ['require'],
    	'plot_id'	=> ['require','check_plot_id'],
    	'a'			=> ['require'],
    	'b'			=> ['require'],
    	'c'			=> ['require'],
    	'imgs'		=> ['require'],
    	'configs'	=> ['require'],
    	'type'		=> ['require','in:1,2'],
    	'decl'		=> ['require'],
    	'direc'		=> ['require','in:1,2,3,4'],
    	'elevator'  => ['require','in:1,2'],
    	'area'		=> ['require'],
    	'time_type'	=> ['require','in:1,2,3'],
    	'price'		=> ['require'],
    	'address'   => ['require'],
    	'house_id'  => ['require','check_house_id'],
    	'tenant_id' => ['require','check_user'],
    	'stime'		=> ['require'],
    	'etime'		=> ['require'],
    	'idcard'	=> ['require','checkIdcard'],
    	'phone' 	=> ['require','checkphone'],
    	'contract_id'=>['require']
    ];
    protected $message=[
    	'tit.require'		=>'房源名称不能为空',
    	'plot_id.require'	=>'房源名称不能为空',
    	'plot_id.check_plot_id'=>'房源名称不能为空',
    	'a.require'			=>'卧室数量不能为空',
    	'b.require'			=>'客厅数量不能为空',
    	'c.require'			=>'卫生间不能为空',
    	'imgs.require'		=>'房源图集不能为空',
    	'configs.require'	=>'房源设备配置不能为空',
    	'type.require'		=>'租赁类型不能为空',
    	'type.in'		    =>'租赁类别不合法',
    	'decl.require'		=>'房源描述不能为空',
    	'direc.require'		=>'房源朝向不能为空',
    	'direc.in'			=>'房源朝向不合法',
    	'elevator.require'	=>'是否有电梯不能为空',
    	'elevator.in'		=>'是否有电梯不合法',
    	'area.require'		=>'房源面积不能为空',
    	'time_type.require' =>'租期类型不能为空',
    	'time_type.in' 		=>'租期类型不合法',
    	'price.require'		=>'租金不能为空',
    	'address.require'	=>'房源地址不能为空',
    	'house_id.require'		=>'房源主键不能为空',
    	'house_id.check_house_id' =>'房源主键无效',
    	'stat.require'		=>'修改状态不能为空',
    	'stat.in'			=>'状态不合法',
    	'tenant_id.require' =>'租客主键不能为空',
    	'tenant_id.check_user'=>'该用户不存在',
    	'stime.require'		=>'租房开始时间不能为空',
    	'etime.require'		=>'租房结束时间不能为空',
    	'contract_id.require'=>'合同主键不能为空'
    ];
    protected $scene=[
    	'put_house'		=>[
    		'tit',
    		'plot_id',
    		'a','b','c',
    		'imgs',
    		'configs',
    		'type','decl','direc','elevator','area','time_type','price','address'],
    	'change_stat'	=>[
    		'house_id'
    	],
    	'write_contract'=>[	//业主发起合同
    		'tenant_id',
    		'house_id',
    		'stime',
    		'etime',
    		'price'
    	],
    	'bill'=>[
    		'contract_id'
    	]
    ];
    //验证手机号
	protected function checkphone($val){
		return check_mobile($val);
	}
	//检查身份证
	protected function checkIdcard($val){
		return isIdCard($val);
	}
    //检查小区主键
    protected function check_plot_id($id){
    	$plot=Db::name('plot')->where(['stat'=>1,'del'=>0])->find();
    	if(!$plot){
    		return false;
    	}else{
    		$this->plot=$plot;
    		return true;
    	}
    }
    //检测房源主键
    protected function check_house_id($id){
    	$house=Db::name('house')->where('id',$id)->find();
    	if(!$house){
    		return false;
    	}elseif($house['del']==1){
    		return false;
    	}else{
    		$this->house=$house;
    		return true;
    	}
    }
    //检查用户主键是否有效
    protected function check_user($uid){
    	$user=Db::name('users')->where('id',$uid)->find();
    	if(!$user || $user['del']==1){
    		return false;
    	}else{
    		$this->user=$user;
    		return true;
    	}
    }
}